<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Dayrui Website Management System
 *
 * @since		version 2.5.0
 * @author		Dayrui <dayrui@gmail.com>
 * @license     http://www.dayrui.com/license
 * @copyright   Copyright (c) 2011 - 9999, Dayrui.Com, Inc.
 */
	
class Pay extends M_Controller {
	
    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
		$this->load->model('pay_model');
    }
	
	/**
     * 在线充值付款跳转
     */
	public function go() {
		if ($data = $this->pay_model->pay_for_online((int)$this->input->get('id'))) {
            if (!isset($data['error'])) {
                if (isset($data['form']) && $data['form']) {
                    $this->member_msg(lang('m-177').'<div style="display:none">'.$data['form'].'</div>', 'javascript:;', 2, 0);
                } elseif (isset($data['url']) && $data['url']) {
                    $this->member_msg(lang('m-177'), $data['url'], 2, 0);
                } else {
                    $this->template->assign(array(
                        'pay' => $data,
                    ));
                    $this->template->display('pay_result.html');
                    exit;
                }
            } else {
                $this->member_msg($data['error']);
            }
		} else {
			$this->member_msg(lang('m-173'));
		}
	}
	
	/**
     * 在线充值
     */
    public function add() {
	
		$money = (double)$this->input->get('money');
		
		if (IS_POST) {
			
			$pay = $this->input->post('pay');
			$money = (double)$this->input->post('money');
			
			if (!$money > 0) {
				$error = lang('m-175');
			} elseif (!$pay) {
				$error = lang('m-176');
			} else {
				if ($data = $this->pay_model->add_for_online($pay, $money)) {
                    if (!isset($data['error'])) {
                        if (isset($data['form']) && $data['form']) {
                            $this->member_msg(lang('m-177').'<div style="display:none">'.$data['form'].'</div>', 'javascript:;', 2, 0);
                        } elseif (isset($data['url']) && $data['url']) {
                            $this->member_msg(lang('m-177'), $data['url'], 2, 0);
                        } else {
                            if (IS_AJAX || IS_JSON) {
                                exit(dr_json(1, $data['html']));
                            }
                            $this->template->assign(array(
                                'pay' => $data,
                            ));
                            $this->template->display('pay_result.html');
                            exit;
                        }
                    } else {
                        $this->member_msg($data['error']);
                    }
				} else {
					$error = lang('m-173');
				}
			}
            if (IS_AJAX || IS_JSON) {
                exit(dr_json(0, $error));
            }
		}
		$this->template->assign(array(
            'pay' => $pay,
			'list' => $this->get_pay_api(1),
			'money' => $money > 0 ? $money : '',
			'result_error' => $error,
		));
		$this->template->display('pay_add.html');
	}
	
	/**
     *  转账服务
     */
    public function transfer() {

        $error = '';
        if (IS_POST) {
            $data = $this->input->post('data');
            $member = $this->db->where('username', dr_safe_replace($data['username']))->get('member')->row_array();
            if (!$member) {
                $error = lang('m-003');
            } elseif ($this->uid == $member['uid']) {
                $error = lang('m-376');
            } else {
                if ($data['type']) {
                    // x
                    $value = abs((int)$data['value']);
                    if ($value <= 0) {
                        $error = lang('m-227');
                    } elseif ($value > $this->member['score']) {
                        $error = dr_lang('m-259', $value, $this->member['score']);
                    } else {
                        $this->member_model->update_score(1, $this->uid, -$value, '', 'lang,m-374,'.$member['username'].','.$value);
                        $this->member_model->update_score(1, $member['uid'], $value, '', 'lang,m-375,'.$this->member['username'].','.$value);
                        $this->member_msg(lang('m-373'), dr_url('pay/score'), 1);
                    }
                } else {
                    // r
                    $value = abs((float)$data['value']);
                    if ($value < 0.01) {
                        $error = lang('m-370');
                    } elseif ($value > $this->member['money']) {
                        $error = dr_lang('m-267', $value, $this->member['money']);
                    } else {
                        $this->pay_model->add($this->uid, -$value, 'lang,m-371,'.$member['username'].','.$value);
                        $this->pay_model->add($member['uid'], $value, 'lang,m-372,'.$this->member['username'].','.$value);
                        $this->member_msg(lang('m-373'), dr_url('pay/index'), 1);
                    }
                }
            }
            if (IS_AJAX || IS_JSON) {
                exit(dr_json(0, $error));
            }
        }

        $this->template->assign(array(
            'data' => $data,
            'result_error' => $error,
        ));
        $this->template->display('pay_transfer.html');

    }

	/**
     * 虚拟卡兑换
     */
    public function convert() {
		
		$error = '';
		if (IS_POST) {
			$score = abs((int)$this->input->post('score'));
			$money = (float)$score/SITE_CONVERT;
			if (!$score) {
				$error = lang('m-227');
			} elseif ($money > $this->member['money']) {
				$error = lang('m-210');
			} else {
				// 虚拟币增加
				$this->member_model->update_score(1, $this->uid, $score, '', 'lang,m-226');
				// 人民币减少
				$this->pay_model->add($this->uid, -$money, 'lang,m-228,'.$score);
				$this->member_msg(lang('m-225'), dr_url('pay/score'), 1);
			}
            if (IS_AJAX || IS_JSON) {
                exit(dr_json(0, $error));
            }
		}
	
		$this->template->assign(array(
			'result_error' => $error,
		));
		$this->template->display('pay_convert.html');
	}
	
	/**
     * 卡密充值
     */
    public function card() {
	
		if (IS_POST) {
			$card = $this->input->post('card', TRUE);
			$password = (int)$this->input->post('password');
			if ($card && $password) {
				$data = $this->db->where('card', $card)->where('password', $password)->get('member_paycard')->row_array();
				if (!$data) {
					$error = lang('m-171');
				} elseif ($data['endtime'] < SYS_TIME) {
					$error = lang('m-169');
				} elseif ($data['uid']) {
					$error = lang('m-170');
				} else {
					if ($money = $this->pay_model->add_for_card($data['id'], $data['money'], $card)) {
						$this->member_msg(dr_lang('m-172', $data['money']), dr_member_url('pay/index'), 1);
					}
					$error = lang('m-172');
				}
			} else {
				$error = lang('m-168');
			}
            if (IS_AJAX || IS_JSON) {
                exit(dr_json(0, $error));
            }
		}
	
		$this->template->assign(array(
			'card' => $card,
			'result_error' => $error,
		));
		$this->template->display('pay_card.html');
	}
	
	/**
     * 充值记录
     */
    public function index() {
	
		$this->db->where('uid', $this->uid)->where('value>0')->order_by('inputtime DESC');
		if ($this->input->get('kw')) {
            $this->db->where('id', (int)$this->input->get('kw'));
        }

        $page = max((int)$this->input->get('page'), 1);
		if ($this->input->get('action') == 'more') {
		    // ajax更多数据
			$data = $this->db->limit($this->pagesize, $this->pagesize * ($page - 1))->get('member_paylog_'.$this->member['tableid'])->result_array();
			if (!$data) {
                exit('null');
            }
			$this->template->assign(array(
				'type' => $this->get_pay_api(1),
				'list' => $data
			));
			$this->template->display('pay_data.html');
		} else {
			$this->template->assign(array(
                'page' => max(2, $page + 1),
				'type' => $this->get_pay_api(1),
				'list' => $this->db->limit($page ? $page * $this->pagesize : $this->pagesize)->get('member_paylog_'.$this->member['tableid'])->result_array(),
				'moreurl' => 'index.php?c='.$this->router->class.'&m='.$this->router->method.'&action=more'
			));
			$this->template->display('pay_index.html');
		}
	}
	
	/**
     * 消费记录
     */
    public function spend() {
	
		$this->db->where('uid', $this->uid)->where('value<0')->order_by('inputtime DESC');
		if ($this->input->get('kw')) {
            $this->db->where('id', (int)$this->input->get('kw'));
        }

        $page = max((int)$this->input->get('page'), 1);
		if ($this->input->get('action') == 'more') {
		    // ajax更多数据
			$data = $this->db->limit($this->pagesize, $this->pagesize * ($page - 1))->get($this->db->dbprefix('member_paylog_'.$this->member['tableid']))->result_array();
			if (!$data) {
                exit('null');
            }
			$this->template->assign('list', $data);
			$this->template->display('pay_data.html');
		} else {
			$this->template->assign(array(
                'page' => max(2, $page + 1),
				'list' => $this->db->limit($page ? $page * $this->pagesize : $this->pagesize)->get($this->db->dbprefix('member_paylog_'.$this->member['tableid']))->result_array(),
				'moreurl' => 'index.php?c='.$this->router->class.'&m='.$this->router->method.'&action=more'
			));
			$this->template->display('pay_index.html');
		}
	}
	
	/**
     * 经验值
     */
    public function experience() {
	
		$this->db->where('uid', $this->uid)->where('type', 0)->order_by('inputtime DESC');

        $page = max((int)$this->input->get('page'), 1);
		if ($this->input->get('action') == 'more') {
		    // ajax更多数据
			$data = $this->db->limit($this->pagesize, $this->pagesize * ($page - 1))->get($this->db->dbprefix('member_scorelog_'.$this->member['tableid']))->result_array();
			if (!$data) {
                exit('null');
            }
			$this->template->assign('list', $data);
			$this->template->display('score_data.html');
		} else {
			$this->template->assign(array(
                'page' => max(2, $page + 1),
				'list' => $this->db->limit($page ? $page * $this->pagesize : $this->pagesize)->get($this->db->dbprefix('member_scorelog_'.$this->member['tableid']))->result_array(),
				'moreurl' => 'index.php?c='.$this->router->class.'&m='.$this->router->method.'&action=more'
			));
			$this->template->display('score.html');
		}
	}
	
	/**
     * 虚拟币
     */
    public function score() {

        $page = max((int)$this->input->get('page'), 1);
		$this->db->where('uid', $this->uid)->where('type', 1)->order_by('inputtime DESC');

		if ($this->input->get('action') == 'more') {
		    // ajax更多数据
			$data = $this->db->limit($this->pagesize, $this->pagesize * ($page - 1))->get($this->db->dbprefix('member_scorelog_'.$this->member['tableid']))->result_array();
			if (!$data) {
                exit('null');
            }
			$this->template->assign('list', $data);
			$this->template->display('score_data.html');
		} else {
			$this->template->assign(array(
                'page' => max(2, $page + 1),
				'list' => $this->db->limit($page ? $page * $this->pagesize : $this->pagesize)->get($this->db->dbprefix('member_scorelog_'.$this->member['tableid']))->result_array(),
				'moreurl' => 'index.php?c='.$this->router->class.'&m='.$this->router->method.'&action=more'
			));
			$this->template->display('score.html');
		}
	}
}